import logging
from urllib.parse import urljoin
from common.send_requests import sendRequest
from Model.environment import Environment
from Model.user import PcUser, AppUser


class Logistics:
    code = 0
    status = None
    logistics_info = None
    plate_number_list = None

    def __init__(self, env, puser, auser, logger: logging.Logger, logistics_no):
        self.env: Environment = env
        self.puser: PcUser = puser
        self.auser: AppUser = auser
        self.logger = logger
        self.headers = self.puser.headers
        self.logistics_no = logistics_no
        self.transport_list = []

    def get_logistics_info(self):
        # 获取物流信息
        url = urljoin(self.env.realmName, "logistics-order")
        params = {
            "orderNo": self.logistics_no,
            "GetTransportOrderCount": True,
            "GetGoodsVehicleCount": True,
            "UseCarTransportData": True,
            "GetTransportNetWeight": True
        }
        res = sendRequest(url, "GET", headers=self.headers,
                          params=params, msg="获取物流信息")
        if res.status_code == 200:
            r = res.json()
            if r.get("sourceNo", None):
                self.code = 1
                self.status = "获取物流信息成功"
                self.logistics_info = r
                print(f"获取物流信息成功: {res.text}")
            else:
                self.code = 0
                self.status = "获取物流信息失败"
                print(f"获取物流信息失败: {res.text}")
        else:
            self.code = 0
            self.status = "获取物流信息异常"
            print(f"获取物流信息异常: {res.text}")

    def get_logistics_plate_number_list(self):
        # 获取物流车牌号列表
        url = urljoin(self.env.realmName, "vehicle/dropdown-list")
        params = {
            "Type": "10",
            "carrierCode": self.logistics_info["carrierCode"],
            "IsNationalVI": True
        }
        res = sendRequest(url, "get", headers=self.headers,
                          params=params, msg="获取物流车牌号列表")
        if res.status_code == 200:
            r = res.json()
            if len(r) > 0:
                self.code = 1
                self.status = "获取物流车牌号列表成功"
                print(f"获取物流车牌号列表成功: {res.text}")
                self.plate_number_list = [i["name"] for i in r]
            else:
                self.code = 0
                self.status = "获取物流车牌列表号失败"
                print(f"获取物流车牌号列表失败: {res.text}")
        else:
            self.code = 0
            self.status = "获取物流车牌列表号异常"
            print(f"获取物流车牌号列表异常: {res.text}")

    def create_transport_order(self, goods_list, plate_number, driver_id_card):
        # 创建装车单
        url = urljoin(self.env.realmName, "logistics-order/vehicle")
        data = {
            "details": [
                {
                    "goodsNo": goods["goods_no"],
                    "allotCount": goods["goods_count"],
                } for goods in goods_list
            ],
            "enterPlateNumber": plate_number,
            "enterDriverIdCard": driver_id_card,
            "plateNumber": plate_number,
            "driverIdCard": driver_id_card,
            "isInvoiceRequired": False,
            "invoiceUnitPrice": None,
            "logisticsOrderNo": self.logistics_no,
            "isCheckCargoAllot": False,
            "isCheckMixture": True,
            "IsCheckEnoughOneCar": True
        }
        print(f"数据: {data}")
        res = sendRequest(url, "post", headers=self.headers,
                          js=data, msg="创建装车单")
        if res.status_code == 200:
            r = res.json()
            if r.get("sourceNo", None):
                self.code = 1
                self.status = "创建装车单成功"
                print(f"创建装车单成功: {res.text}")
            else:
                self.code = 0
                self.status = "创建装车单失败"
                print(f"创建装车单失败: {res.text}")
        else:
            self.code = 0
            self.status = "创建装车单异常"
            print(f"创建装车单异常: {res.text}")

    def get_logistics_transport_order(self):
        # 获取物流单下已创建的装车单
        url = urljoin(self.env.realmName, "transport-order/search")
        params = {
            "LogisticsOrderNo": self.logistics_no,
            "MaxResultCount": 100,
            "SkipCount": 0
        }
        res = sendRequest(url, "get", headers=self.headers,
                          params=params, msg="获取物流单下已创建的装车单")
        if res.status_code == 200:
            r = res.json()
            if r.get("items", None):
                if len(r["items"]) == 0:
                    self.code = 0
                    self.status = "未创建装车单"
                else:
                    # 装车单列表
                    self.code = 1
                    self.transport_list = [{
                        "transport_no": transport_info["transportOrderNo"],
                        "status": transport_info["transportStatusTypeName"],
                        "create_time": transport_info["creationTime"],
                    } for transport_info in r["items"]]
                    print(f"获取物流单下已创建的装车单成功: {res.text}")
            else:
                self.code = 0
                self.status = "获取物流单下已创建的装车单失败"
                print(f"获取物流单下已创建的装车单失败: {res.text}")
        else:
            self.code = 0
            self.status = "获取物流单下已创建的装车单异常"
            print(f"获取物流单下已创建的装车单异常: {res.text}")
